package com.xyzisme76.application;

import com.xyzisme76.dao.ScDao;
import com.xyzisme76.dao.StudentDao;
import com.xyzisme76.pojo.Sc;
import com.xyzisme76.pojo.Student;
import com.xyzisme76.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class SqlSelectInfo {
    private static final int WIDTH =500;
    private static final int HEIGHT =400;
    private static int selectFunc = 0;
    SqlSelectInfo(){
        sqlSelectObm();
    }
    void sqlSelectObm(){
        JFrame jf = new JFrame("成绩查询");


        jf.setSize(new Dimension(WIDTH,HEIGHT));
        //jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //测试时方便快速退出

        // 所在位置 居中
        jf.setLocationRelativeTo(null);
        //设置背景图片
        JPanel jp = new JPanel() {
            protected void paintComponent(Graphics g) {
                try {
                    Image bg = ImageIO.read(new File(
                            "src/main/java/com/xyzisme76/application/images/Search2backf.png"));
                    g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
        };

        JTextArea jta = new JTextArea("请输入学号：");
        JTextField jtf = new JTextField(20);
//        JTextArea infor = new JTextArea();
        JButton seach = new JButton("查询");
        seach.setForeground(Color.DARK_GRAY);
        seach.setBackground(Color.white);
        seach.setFont(new Font("微软雅黑", Font.BOLD, 15));
        jta.setFont(new Font("微软雅黑", Font.BOLD, 13));
        JTable table = new JTable();
        String[] header = {"课程号", "课程","分数"};
        TableModel tableModel = new DefaultTableModel(null, header);
        table.setModel(tableModel);
        seach.addActionListener(e -> {
            String searchIn =  jtf.getText();
            List<Sc> scList =  GetGrade.getIt(searchIn);
            int len=scList.size();
            Object[][] rowData = new Object[len][3];
            int j=0;
            for(Sc sc:scList){
                rowData[j][0]=sc.getCno();
                rowData[j][1]=CourseInfo.courseNameGet(sc.getCno());
                rowData[j][2]=sc.getGrade();
                j++;
            }
            TableModel tableModel1 = new DefaultTableModel(rowData, header);
            table.setModel(tableModel1);
        });

        jp.add(seach);
        jp.add(jta);
        jp.add(jtf);
        jp.add(table.getTableHeader());
        jp.add(table);
        table.setBounds(80, 180, 300, 600);
        table.getTableHeader().setBounds(80,150,300,30);
        jf.add(jp);
        jp.setLayout(null);
        jta.setBounds(70, 72, 400, 30);
        //infor.setBounds(100,100,400,300);
        jtf.setBounds(78,108,260,30 );
        seach.setBounds(350,108,75,30);
        jf.setVisible(true);

    }
}

class  GetGrade{
    static List<Sc> getIt(String Sno){

        SqlSession sqlSession = MybatisUtil.getSqlSession();
        ScDao scDao = sqlSession.getMapper(ScDao.class);
        List<Sc> scList = scDao.getGradeBySno(Sno);
        for (Sc student :scList){
            System.out.println(student);
        }
//        Sc sc = scDao.getGradeBySno(sno);
        sqlSession.close();
        return scList;
    }

}

class GetInfomation {
    static Student getSt(String Sno){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Student student = studentDao.getStudentBySno(Sno);
        System.out.println(student);
//        Sc sc = scDao.getGradeBySno(sno);
        sqlSession.close();
        return student;
    }
}


